//
//  CZRetweetView.swift
//  Weibo24
//
//  Created by apple on 16/3/1.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import SnapKit

/*
    1.数据--也就是接口文档
    2.分析页面 -- 添加一个Label
    3.实现功能 -- 
*/
class CZRetweetView: UIView {

    //记录一个约束
    var bottomConstaint: Constraint?
    
    //定义一个属性
    var retweetViewModel: CZStatusViewModel?{
    
        didSet{
            
//            contentLabel.text = retweetViewModel?.retweetStatusString
            if let text = retweetViewModel?.retweetStatusString{
            
                contentLabel.attributedText = retweetViewModel?.getAttributeString(text)
            }
            
            //数据这里来判断,如果有转发的配图则显示转发配图,
            //没有转发配图,则不显示配图
            
            //转发微博的图片都在转发微博字段里
            
            //清空原有的约束
            self.bottomConstaint?.uninstall()
            
            if let pic_url = retweetViewModel?.statusModel?.retweeted_status?.pic_urls where pic_url.count > 0{
            
                pictureView.hidden = false
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                    
                })
                
                //传值
                
//                pictureView.pictureViewModel = retweetViewModel
                
                //需要把 model转换成ViewModel
                
                //定义一个临时数组进行数据拼接
                var result = [CZPictureViewModel]()
                for model in pic_url{
                    
                    let viewModel  = CZPictureViewModel()
                    
                    viewModel.model = model
                    
                    result.append(viewModel)
                }
                
                pictureView.pictureList = result
                
            } else {
                
                pictureView.hidden = true
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstaint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                    
                })
            }
        }
        
    }
    
    override init(frame: CGRect) {

        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        
    }

    
    private func setupUI(){
    
        backgroundColor = UIColor.yellowColor()
        
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left)
            make.top.equalTo(self.snp_top)
            make.right.equalTo(self.snp_right)
//            make.bottom.equalTo(self.snp_bottom)
            
        }
        
        //默认认为 都有转发配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom)
            
            //给它一个固定的宽高
            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        //转发微博视图的底部在哪里???
        self.snp_makeConstraints { (make) -> Void in
          self.bottomConstaint =  make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
        
    }
    
    //MARK:--懒加载
    private lazy var contentLabel: UILabel = {
    
        let label = UILabel()
        
        label.text = "转发微博"
        
        label.textColor = UIColor.redColor()
        
        label.numberOfLines = 0
        
        label.font = UIFont.systemFontOfSize(14)
        
        return label
        
    }()
    
    //
    lazy var pictureView: CZPictureView = CZPictureView()
}
